package com.xingyun.mvp.ui.home.adapter;

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xingyun.R;
import com.xingyun.app.data.entity.TodayArticleResponse;

import java.util.List;

/**
 * Created by Cyw on 2017/11/23.
 */

public class MainPagerAdapter extends RecyclerView.Adapter<MainPagerAdapter.ViewHolder> {

    private List<TodayArticleResponse.DataBean> datalists;

    public MainPagerAdapter(List<TodayArticleResponse.DataBean> datalists) {
        this.datalists = datalists;
    }

    @Override
    public MainPagerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_list_main,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MainPagerAdapter.ViewHolder holder, int position) {
        holder.tv_title.setText(datalists.get(position).getTitle());
        holder.tv_author.setText(datalists.get(position).getAuthor());
        holder.tv_digest.setText("\u3000\u3000"+datalists.get(position).getDigest());

        View itemView = ((CardView) holder.itemView).getChildAt(0);
        if (mOnItemClickListener != null) {
            itemView.setOnClickListener(v -> {
                int position1 = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(datalists.get(position1));
            });
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_title;
        TextView tv_author;
        TextView tv_digest;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_author = itemView.findViewById(R.id.tv_author);
            tv_digest = itemView.findViewById(R.id.tv_digest);
        }
    }

    @Override
    public int getItemCount() {
        return datalists == null ? 0 : datalists.size();
    }


    private OnItemClickListener mOnItemClickListener;//声明接口

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(TodayArticleResponse.DataBean dataBean);
    }

}
